package ru.amse.baltijsky.javascheme.exporter.image;

import java.util.ArrayList;
import java.util.List;

/**
 * Tools for wrapping the strings containing some source code.
 */
public class StringWrapper {
    /**
     * Wraps the given string to substrings of length not more than the specified.
     *
     * @param s      the string to wrap
     * @param maxLen maximum length of an unwrapped string
     * @return list of the resulting substrings
     */
    public static List<String> wrap(String s, int maxLen) {
        ArrayList<String> res;
        String[] splitters = new String[]{"||", "&&", "|", "^", "&", "==", "!=",};
//        for(Iterator<String> si = res.iterator(); si.hasNext(); ) {
//            if(sAnd.length() > maxLen) {
//
//            }
//        }

        //return res;
        res = new ArrayList<String>();
        res.add(s);
        return res;
    }

    /**
     * Wraps the given string via splitting it by the given splitter symbols until
     * the parts of the string are of not more length than the specified.
     *
     * @param toSplit  the string to split
     * @param maxLen   maximum length for the unsplitted string
     * @param splitter the split string
     * @return the resulting parts of the string
     */
    private static ArrayList<String> split(String toSplit, int maxLen, String splitter) {
        ArrayList<String> ret = new ArrayList<String>();
        ret.add(toSplit);
        String[] splitted = toSplit.split(splitter);
        StringBuilder collector = new StringBuilder();
        for (String s : splitted) {
            if (collector.length() + s.length() <= maxLen) {
                collector.append(" ").append(splitter).append(" ").append(s);
            } else {
                ret.add(collector.toString());
                collector = new StringBuilder(s);
            }
        }
        if (!(collector.length() == 0)) {
            ret.add(collector.toString());
        }

        return ret;
    }
}
